package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseBindingRecyclerViewAdapter;
import com.cloudcreativity.blyg.databinding.ItemLayoutAreaBinding;
import com.cloudcreativity.blyg.databinding.ItemLayoutCityBinding;
import com.cloudcreativity.blyg.databinding.ItemLayoutProvinceBinding;
import com.cloudcreativity.blyg.databinding.LayoutDialogAreaChooserBinding;
import com.cloudcreativity.blyg.sqlite.Area;
import com.cloudcreativity.blyg.sqlite.City;
import com.cloudcreativity.blyg.sqlite.Province;
import com.cloudcreativity.blyg.sqlite.utils.AreaDaoUtils;

import java.util.List;

public class AreaChooseUtils extends Dialog {

    public BaseBindingRecyclerViewAdapter<Province, ItemLayoutProvinceBinding> provinceAdapter;
    public BaseBindingRecyclerViewAdapter<City, ItemLayoutCityBinding> cityAdapter;
    public BaseBindingRecyclerViewAdapter<Area, ItemLayoutAreaBinding> areaAdapter;

    private Province currentProvince;
    private City currentCity;
    private Area currentArea;
    private final LayoutDialogAreaChooserBinding chooserBinding;
    private OnOkListener onOkListener;

    public void setOnOkListener(OnOkListener onOkListener) {
        this.onOkListener = onOkListener;
    }

    public AreaChooseUtils(@NonNull Context context) {
        super(context, R.style.myProgressDialogStyle);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        chooserBinding = DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.layout_dialog_area_chooser,
                null,false);
        setContentView(chooserBinding.getRoot());
        chooserBinding.setUtils(this);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = context.getResources().getDisplayMetrics().widthPixels/2;
        //先加载全部的省数据
        List<Province> provinces = new AreaDaoUtils(context).queryProvinces();
        provinceAdapter = new BaseBindingRecyclerViewAdapter<Province, ItemLayoutProvinceBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_province;
            }

            @Override
            protected void onBindItem(ItemLayoutProvinceBinding binding, final Province item, int position) {
                binding.setItem(item);
                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(item==currentProvince)return;
                        //加载市区数据
                        List<City> cities = new AreaDaoUtils(context).queryCities(item.getId());
                        cityAdapter.getItems().clear();
                        areaAdapter.getItems().clear();
                        cityAdapter.getItems().addAll(cities);
                        currentProvince = item;
                        chooserBinding.tvResult.setText("("+currentProvince.getName()+")");
                    }
                });
            }
        };
        cityAdapter = new BaseBindingRecyclerViewAdapter<City, ItemLayoutCityBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_city;
            }

            @Override
            protected void onBindItem(ItemLayoutCityBinding binding, final City item, int position) {
                binding.setItem(item);
                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //加载市区数据
                        if(item==currentCity) return;
                        List<Area> areas = new AreaDaoUtils(context).queryAreas(item.getId());
                        areaAdapter.getItems().clear();
                        areaAdapter.getItems().addAll(areas);
                        currentCity = item;
                        chooserBinding.tvResult.setText("("+currentProvince.getName()+"/"+currentCity.getName()+")");
                    }
                });
            }
        };
        areaAdapter = new BaseBindingRecyclerViewAdapter<Area, ItemLayoutAreaBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_area;
            }

            @Override
            protected void onBindItem(ItemLayoutAreaBinding binding, final Area item, int position) {
                binding.setItem(item);
                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        currentArea = item;
                        chooserBinding.tvResult.setText("("+currentProvince.getName()+
                                "/"+currentCity.getName()+
                                "/"+currentArea.getName()
                                +")");
                    }
                });
            }
        };
        provinceAdapter.getItems().addAll(provinces);
        chooserBinding.rcvProvince.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL,false));
        chooserBinding.rcvCity.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL,false));
        chooserBinding.rcvArea.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL,false));
    }

    public void onCancel(){
        dismiss();
    }

    public void onOk(){
        if(onOkListener!=null){
            if(currentProvince==null){
                ToastUtils.showShortToast(getContext(),"请选择省");
                return;
            }
            if(currentCity==null){
                ToastUtils.showShortToast(getContext(),"请选择省");
                return;
            }
            if(currentArea==null){
                ToastUtils.showShortToast(getContext(),"请选择省");
                return;
            }
            onOkListener.onOk(currentProvince,currentCity,currentArea);
        }
        dismiss();
    }

    public interface OnOkListener{
        public void onOk(Province p,City c,Area a);
    }
}
